package cn.wolfcode.wolf2w.service.impl;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private  StrategyCommentRepository strategyCommentRepository;

    @Autowired

    private MongoTemplate mongoTemplate;
    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public StrategyComment get(String id) {
        return strategyCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return strategyCommentRepository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        Query query = new Query();

        query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));

        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1,qo.getPageSize(), Sort.Direction.DESC,"createTime");

        return DBHelper.query(mongoTemplate,pageable,query,StrategyComment.class,qo);
    }

    @Override
    public void commentThumb(String cid, String sid, String userInfoId) {
        StrategyComment comment = this.get(cid);
        List<String> thumbuplist = comment.getThumbuplist();
        if (thumbuplist.contains(userInfoId)) {
            thumbuplist.remove(userInfoId);
            comment.setThumbupnum(comment.getThumbupnum() - 1);
            this.update(comment);
        }  else {
            thumbuplist.add(userInfoId);
            comment.setThumbupnum(comment.getThumbupnum() + 1);
            this.update(comment);
        }
    }
    @Override
    public void addComment(StrategyComment comment, UserInfo userInfo) {

        //参数一源对象  参数二目标对象,源对象属性名必须跟目标对象属性名一致时才可以属性值复制
        comment.setUserId(userInfo.getId());

        BeanUtils.copyProperties(userInfo,comment);

        comment.setCreateTime(new Date());

        this.save(comment);
    }
}
